package org.zhengquan.service;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.zhengquan.common.LogType;
import org.zhengquan.common.RequestHolder;
import org.zhengquan.dao.SysLogMapper;
import org.zhengquan.dao.SysRoleUserMapper;
import org.zhengquan.dao.SysUserMapper;
import org.zhengquan.pojo.SysLogWithBLOBs;
import org.zhengquan.pojo.SysRoleUser;
import org.zhengquan.pojo.SysUser;
import org.zhengquan.util.IpUtil;
import org.zhengquan.util.JsonMapper;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @author zhengquan
 * @date 2017/11/5
 */
@Service
public class SysRoleUserService {
    @Autowired
    private SysRoleUserMapper sysRoleUserMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysLogMapper sysLogMapper;

    public List<SysUser> selectListByRoleId(Integer roleId) {
        List<Integer> userIdList = sysRoleUserMapper.selectUserIdListByRoleIdList(Lists.newArrayList(roleId));
        if (CollectionUtils.isEmpty(userIdList)) {
            return Lists.newArrayList();
        }
        return sysUserMapper.selectByIdList(userIdList);
    }

    public void changeRoleUsers(Integer roleId, List<Integer> userIdList) {
        List<Integer> orginUserIdList = sysRoleUserMapper.selectUserIdListByRoleIdList(Lists.newArrayList(roleId));
        if (orginUserIdList.size() == userIdList.size()) {
            Set<Integer> originUserIdSet = Sets.newHashSet(orginUserIdList);
            Set<Integer> userIdSet = Sets.newHashSet(userIdList);
            originUserIdSet.removeAll(userIdSet);
            if (CollectionUtils.isEmpty(orginUserIdList)) {
                return;
            }
        }
        updateRoleUsers(roleId, userIdList);
        saveRoleUserLog(roleId,orginUserIdList,userIdList);
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateRoleUsers(Integer roleId, List<Integer> userIdList) {
        sysRoleUserMapper.deleteByRoleId(roleId);
        if (CollectionUtils.isEmpty(userIdList)) {
            return;
        }
        List<SysRoleUser> roleUserList = Lists.newArrayList();
        for (Integer userId : userIdList) {
            SysRoleUser sysRoleUser = SysRoleUser.builder().roleId(roleId).userId(userId)
                    .operator(RequestHolder.getCurrentUser().getUsername())
                    .operateIp(IpUtil.getRemoteIp(RequestHolder.getCurrentRequest()))
                    .operateTime(new Date()).build();
            roleUserList.add(sysRoleUser);
        }
        sysRoleUserMapper.batchInsert(roleUserList);
    }

    private void saveRoleUserLog(int roleId, List<Integer> before, List<Integer> after) {
        SysLogWithBLOBs sysLogWithBLOBs = new SysLogWithBLOBs();
        sysLogWithBLOBs.setType(LogType.TYPE_ROLE_USER);
        sysLogWithBLOBs.setTargetId(roleId);
        sysLogWithBLOBs.setOldValue(before == null ? "" : JsonMapper.obj2Str(before));
        sysLogWithBLOBs.setNewValue(after == null ? "" : JsonMapper.obj2Str(after));
        sysLogWithBLOBs.setOperator(RequestHolder.getCurrentUser().getUsername());
        sysLogWithBLOBs.setOperateTime(new Date());
        sysLogWithBLOBs.setOperateIp(IpUtil.getRemoteIp(RequestHolder.getCurrentRequest()));
        sysLogMapper.insertSelective(sysLogWithBLOBs);
    }
}
